SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF
GO
CREATE PROCEDURE [dbo].[paChequearPermiso]

@Permiso AS VARCHAR(100),
@Valido BIT OUTPUT

AS
SET NOCOUNT ON
DECLARE @EsValido AS BIT

SELECT @EsValido = COUNT(*) FROM tb_Permisos PER
	INNER JOIN tb_relUsuarioPermisos RUP ON PER.IdPermiso = RUP.IdPermiso
	INNER JOIN tb_Usuarios USU ON USU.IdUsuario = RUP.IdUsuario
	WHERE USU.NTUser = SYSTEM_USER AND PER.Permiso = @Permiso

SELECT @EsValido = @EsValido + COUNT(*)
	FROM tb_Permisos PER
	INNER JOIN tb_relPermisosRoles RPR ON RPR.IdPermiso = PER.IdPermiso
	INNER JOIN tb_Roles ROL ON ROL.IdRol = RPR.IdRol
	INNER JOIN tb_relUsuarioRol RUR ON RUR.IdRol = ROL.IdRol
	INNER JOIN tb_Usuarios USU ON USU.IdUsuario = RUR.IdUsuario
	WHERE USU.NTUser = SYSTEM_USER AND PER.Permiso = @Permiso

SET @Valido = @EsValido
GO
GRANT EXECUTE ON  [dbo].[paChequearPermiso] TO [public]
GO
